package com.leo.guppao.strategy;

import com.leo.guppao.enums.VipEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author liusl
 * @date 2019/3/13-13:54
 */
@Component
public class StrategyContext {
    private final Map<String, IStrategy> strategyMap = new ConcurrentHashMap<>();

    @Autowired
    public StrategyContext(Map<String, IStrategy> strategyMap) {
        this.strategyMap.clear();
        strategyMap.forEach(this.strategyMap::put);
    }

    /**
     * 计算价格
     *
     * @param memberLevel 会员等级
     * @return 价格
     */
    public BigDecimal calculatePrice(VipEnum memberLevel) {
        return strategyMap.get(memberLevel.getName()).calculatePrice();
    }
}
